Resource Routing

  • STEP

    1. Normal Route

    
                        use App\Http\Controllers\BlogController;
    
    
    
                      Route::get('blogs', '[BlogController::class, 'index']');
    
                      Route::get('blogs/create', '[BlogController::class, 'create']');
    
                      Route::post('blogs', '[BlogController::class, 'store']');
    
                      Route::get('blogs/{blog}/edit', '[BlogController::class, 'edit']');
    
                      Route::put('blogs/{blog}', '[BlogController::class, 'update']');
    
                      Route::get('blogs/{blog}', '[BlogController::class, 'show']');
    
                      Route::delete('blogs/{blog}', '[BlogController::class, 'destroy']');
    
                      

    As you can see above route declare, we have to create six routes for our crud application module. But we can simply create those six routes by using bellow resource route:

    2. Resource Route:

    in routes/web.php

    
                      use App\Http\Controllers\BlogController;
                      Route::resource('blogs', BlogController::class);
                      

    3. show route lists

    run bellow command

    
                       php artisan route:list --name=blogs
    
    Output

    +--------+-----------+-------------------+---------------+---------------------------------------------+--------------+

    | Domain | Method | URI | Name | Action | Middleware |

    +--------+-----------+-------------------+---------------+---------------------------------------------+--------------+

    | | GET|HEAD | api/user | | Closure | api,auth:api |

    | | GET|HEAD | blogs | blogs.index | App\Http\Controllers\BlogController@index | web |

    | | POST | blogs | blogs.store | App\Http\Controllers\BlogController@store | web |

    | | GET|HEAD | blogs/create | blogs.create | App\Http\Controllers\BlogController@create | web |

    | | GET|HEAD | blogs/{blog} | blogs.show | App\Http\Controllers\BlogController@show | web |

    | | PUT|PATCH | blogs/{blog} | blogs.update | App\Http\Controllers\BlogController@update | web |

    | | DELETE | blogs/{blog} | blogs.destroy | App\Http\Controllers\BlogController@destroy | web |

    | | GET|HEAD | blogs/{blog}/edit | blogs.edit | App\Http\Controllers\BlogController@edit | web |

    +--------+-----------+-------------------+---------------+---------------------------------------------+--------------+

    3. Ccreate Resource Controller

    
    php artisan make:controller BlogController --resource
    

    app/Http/Controllers/BlogController.php

    
    namespace App\Http\Controllers;
    
    use Illuminate\Http\Request;
    
    class BlogController extends Controller
    {
       
        public function index()
        {
            //
        }
    
      
        public function create()
        {
            //
        }
    
     
        public function store(Request $request)
        {
            //
        }
    
     
        public function show($id)
        {
            //
        }
    
    
        public function edit($id)
        {
            //
        }
    
       
        public function update(Request $request, $id)
        {
            //
        }
    
      
        public function destroy($id)
        {
            //
        }
    }